package newzhihu.mzxy.myapplication;


import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.databinding.BindingAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import newzhihu.mzxy.myapplication.databinding.ActivityMainBinding;
import newzhihu.mzxy.myapplication.databinding.ListItemBinding;
import newzhihu.mzxy.myapplication.moder.ListModer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    SmartRefreshLayout smartRefreshLayout;
    public ArrayList<ListModer.Stories> list=new ArrayList<>();
    RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        recyclerView=findViewById(R.id.recycleview);
        smartRefreshLayout=findViewById(R.id.refreshLayout);
        recyclerView.setAdapter(new MyAdpater());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                Call<ListModer> listModerCall=RetrofitUtils.getRetrofit().create(Apis.class).getList();
                listModerCall.enqueue(new Callback<ListModer>() {
                    @Override
                    public void onResponse(Call<ListModer> call, Response<ListModer> response) {
                        smartRefreshLayout.finishRefresh();
                        list.clear();
                        list=response.body().stories;
                        recyclerView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<ListModer> call, Throwable t) {

                    }
                });

            }
        });

        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                Call<ListModer> listModerCall=RetrofitUtils.getRetrofit().create(Apis.class).getList();
                listModerCall.enqueue(new Callback<ListModer>() {
                    @Override
                    public void onResponse(Call<ListModer> call, Response<ListModer> response) {
                        smartRefreshLayout.finishLoadMore();
                        list.clear();
                        list.addAll(response.body().stories);
                        recyclerView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(Call<ListModer> call, Throwable t) {

                    }
                });

            }
        });


    }
    public class MyAdpater extends RecyclerView.Adapter<MyViewHolder>{

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            ListItemBinding binding= DataBindingUtil.inflate
                    (LayoutInflater.from(MainActivity.this),R.layout.list_item,viewGroup,false);
//            ActivityMainBinding binding=new DataBindingUtil(LayoutInflater.from(MainActivity.this),R.layout.list_item,viewGroup,false);

//            MyViewHolder myViewHolder=new MyViewHolder();

            return new MyViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
            myViewHolder.listItemBinding.setVariable(BR.data,list.get(i));
            myViewHolder.listItemBinding.setVariable(BR.myclick,new Myclick());

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

    }
    @BindingAdapter("url")
    public static void setImgurl(ImageView imageView,String url){
        Glide.with(imageView.getContext()).load(url).into(imageView);

    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
        ListItemBinding listItemBinding;
        public MyViewHolder(@NonNull ListItemBinding itemView) {
            super(itemView.getRoot());
            this.listItemBinding=itemView;
        }
    }
    public class Myclick{
        public void myclick1(View view){
            Log.i("Myclick","onclick");

        }
    }
}
